/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2012-10-23         luwei
 */

package com.tianci.appstore.ui.search;

import android.content.Context;
import android.view.View;

import com.coocaa.x.uipackage.widget.SlideFocusView;
import com.tianci.appstore.ui.data.SkyAppBaseData;
import com.tianci.appstore.ui.widget.SkyBaseItemView;
import com.tianci.appstore.ui.widget.SkyBaseItemView.OnAppKeyListener;
import com.tianci.appstore.ui.widget.SkyGridAdapter;
import com.tianci.appstore.utils.SkyConstants;

import java.util.ArrayList;
import java.util.List;

public class SkySearchHotAdapter extends SkyGridAdapter {
    private int mIndex = 0;
    protected int count = 0;
    protected OnAppKeyListener mKeyListener = null;
    private SlideFocusView.FocusChangedEvent mFocusChangedEvent = null;
    private SlideFocusView.FocusViewRevision mRevision = null;

    public SkySearchHotAdapter(Context context) {
        super(context, 1, null);
    }

    public void setKeyListener(OnAppKeyListener listener) {
        this.mKeyListener = listener;
    }

    public void setFocusChangedEvent(SlideFocusView.FocusChangedEvent focusChangedEvent) {
        this.mFocusChangedEvent = focusChangedEvent;
        mRevision = new SlideFocusView.FocusViewRevision(SkyConstants.Div(0), SkyConstants.Div(0));
    }

    @Override
    public int getCount() {
        return count;
    }

    @Override
    public int getPosition() {
        return mIndex;
    }

    @Override
    public View getView(int position, View convertView, boolean[] turnpage) {
        SkyBaseItemView appView = null;
        if (convertView == null) {
            appView = new SearchHotItem(mContext);
            appView.setKeyListener(mKeyListener);
            mAppItemList.add(appView);
            if (mFocusChangedEvent != null) {
                mFocusChangedEvent.registerView(appView, mRevision, appView.getOnFocusChangeListener());
                appView.setBackgroundDrawable(null);
            }
        } else {
            appView = (SkyBaseItemView) convertView;
        }
        appView.update(mAppList.get(position));
        appView.setIndex(position);
        appView.setTurnPage(turnpage);
        return appView;
    }

    @Override
    public void refreshUI(List<SkyAppBaseData> datas) {
        if (datas == null) {
            datas = new ArrayList<SkyAppBaseData>();
        }
        this.mAppList = datas;
        this.count = mAppList.size();
        notifyDataSetChanaged();
    }

    @Override
    public void destory() {
        // TODO Auto-generated method stub

    }
}
